//index.js
//获取应用实例
const app = getApp()
var openid = ''
var TopData = {}
var UserInfodata = {}
var openidData = {}
var nickname = null
var head = null
var url = wx.getStorageSync('url')
var openid = wx.getStorageSync('openid')
var userId = wx.getStorageSync('userId')
var shopid = wx.getStorageSync('shopid')
function getNowFormatDate() {
  var dates = new Date();
  var year = dates.getFullYear();
  var month = dates.getMonth() + 1;
  var date = dates.getDate();
  var hour = dates.getHours() < 10 ? "0" + dates.getHours() : dates.getHours();
  var minute = dates.getMinutes() < 10 ? "0" + dates.getMinutes() : dates.getMinutes();
  var second = dates.getSeconds() < 10 ? "0" + dates.getSeconds() : dates.getSeconds();
  // this.milliSeconds = date.getMilliseconds();
  var currentTime = year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':' + second
  return currentTime;
  
}
var time_ss = getNowFormatDate()
wx.setStorageSync('time_ss', time_ss)
Page({
  data: {
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    pop:false
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onShow: function () {   
    var time_ss = getNowFormatDate()
    wx.setStorageSync('time_ss', time_ss)

    var that = this
    // wx.showLoading({
    //   title: '加载中',
    // })
    // 首页轮播
    wx.request({
      url: url+'gateShop/mpSearchShopAttr',
      data: {
        "list": false,
        "nodeId": "kdslfw",
        "platform": 2,
        "privId": "default",
        "resId": "default",
        "sendTime": time_ss,
        "seq": 1,
        "shopId": shopid,
        "source": 11,
        "sysVer": "1.0.0",
        "token": "kdsjfoewi12ijsldks",
        "type": 1,
        "userId": "0201beaa92dd1f1e4d58b5b2b54af559c3b8",
        "userType": 1,
        "content": {
          "shopId": shopid,
          "dist": "chief_banner"
        }
      },
      method: "POST",
      header: { "Content-Type": "application/JSON" },
      success: function (e) {
        var data = e.data.content
        that.setData({
          banler: data
        })
      }
    })
// 静态图
    wx.request({
      url: url + 'gateShop/mpSearchShopAttr',
      data: {
        "platform": 2,
        "privId": "0000",
        "resId": "0000",
        "sendTime": time_ss,
        "seq": "232312313",
        "shopId": shopid,
        "source": 1,
        "sysVer": "0.0.1",
        "type": 1,
        "userId": '0201460a349f504c4c4a9bbe70887ea22fa1',
        "userType": 1,
        "content": { "shopId": shopid, "dist": "chief_middle" }
      },
      method: "POST",
      header: { "Content-Type": "application/JSON" },
      success: function (e) {
        var data = e.data.content
        that.setData({
          middle: data[0]
        })
      }
    })
// 流星降
    wx.request({
      url: url + 'gateShop/mpSearchShopAttr',
      data: {
        "platform": 2,
        "privId": "0000",
        "resId": "0000",
        "sendTime": time_ss,
        "seq": "232312313",
        "shopId": shopid,
        "source": 1,
        "sysVer": "0.0.1",
        "type": 1,
        "userId": '0201460a349f504c4c4a9bbe70887ea22fa1',
        "userType": 1,
        "content": { "shopId": shopid, "dist": "lxj" }
      },
      method: "POST",
      header: { "Content-Type": "application/JSON" },
      success: function (e) {
        var data = e.data.content
        that.setData({
          lxj: data
        })
      }
    })
    // 颜值杀
    wx.request({
      url: url + 'gateShop/mpSearchShopAttr',
      data: {
        "platform": 2,
        "privId": "0000",
        "resId": "0000",
        "sendTime": time_ss,
        "seq": "232312313",
        "shopId": shopid,
        "source": 1,
        "sysVer": "0.0.1",
        "type": 1,
        "userId": '0201460a349f504c4c4a9bbe70887ea22fa1',
        "userType": 1,
        "content": { "shopId": shopid, "dist": "yzs" }
      },
      method: "POST",
      header: { "Content-Type": "application/JSON" },
      success: function (e) {
        wx.hideLoading()
        var data = e.data.content
        that.setData({
          yzs: data
        })
      }
    })
    // 系统信息
    wx.request({
      url: url + 'gateShop/getSysMsg',
      data: {
        "platform": 2,
        "privId": "0000",
        "resId": "0000",
        "sendTime": time_ss,
        "seq": "232312313",
        "shopId": shopid,
        "source": 1,
        "sysVer": "0.0.1",
        "type": 1,
        "userId": '0201460a349f504c4c4a9bbe70887ea22fa1',
        "userType": 1,
        "content": {
          "shopId": shopid
        }
      },
      method: "POST",
      header: { "Content-Type": "application/JSON" },
      success: function (e) {
        wx.hideLoading()
        var data = e.data.content
        that.setData({
          ss: data
        })
      }
    })
    // 授权
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          //发起网络请求
          console.log(res.code)
          wx.request({
            url: url + 'sharkUser/find/openid',
            data: {
              "type": 1,
              "seq": 1,
              "sysVer": "0.0.1",
              "sendTime": time_ss,
              "userId": "",
              "resId": "0000",
              "privId": "0000",
              "platform": 2,
              "token": "",
              "shopId": "",
              "userType": 3,
              "source": 4,
              "content": {
                "appid": "wx6ca4bda138687ed8",
                "secret": "81b33d96dfbe809cff410a03b54d4c00",
                "usercode": res.code
              }
            },
            method: "POST",
            header: { "Content-Type": "application/JSON" },
            success: function (e) {
              console.log(e)
              openid = e.data.content.openid;
              wx.setStorageSync('openid', openid)
              wx.getUserInfo({
                success: function (e) {
                  nickname = e.userInfo.nickName
                  head = e.userInfo.avatarUrl
                  UserInfodata.avatarUrl = e.userInfo.avatarUrl,
                    UserInfodata.nickName = e.userInfo.nickName,
                    wx.setStorageSync('UserInfodata', UserInfodata)
                  wx.request({
                    url: url + 'sharkUser/wxLogin',
                    data: {
                      "type": 1,
                      "seq": 1,
                      "sysVer": "0.0.1",
                      "sendTime": time_ss,
                      "accountType": 1,
                      "userId": "default",
                      "resId": "0000",
                      "privId": "0000",
                      "platform": 2,
                      "token": "",
                      "shopId": shopid,
                      "userType": 1,
                      "source": 4,
                      "content": {
                        "openid": openid,
                        "username": nickname,
                        "shopId": shopid,
                        "image": head
                      }
                    },
                    method: "POST",
                    header: {
                      "Content-Type": "application/JSON"
                    },
                    success: function (res) {
                      var userId = res.data.userId
                      wx.setStorageSync('userId', userId)
                    }
                  })
                }, fail: function (failData) {
                  wx.hideTabBar({

                  })
                  that.setData({
                    pop:true
                  })
                }
              })

            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  // 获取授权按钮
  bindGetUserInfo: function (e) {
    var time_ss = getNowFormatDate()
    wx.setStorageSync('time_ss', time_ss)
    var that = this;
    openid= wx.getStorageSync('openid')
    //此处授权得到userInfo
    //接下来写业务代码
    nickname = e.detail.userInfo.nickName
    head = e.detail.userInfo.avatarUrl
    wx.request({
      url: url + 'sharkUser/wxLogin',
      data: {
        "type": 1,
        "seq": 1,
        "sysVer": "0.0.1",
        "sendTime": time_ss,
        "userId": "default",
        "resId": "0000",
        "privId": "0000",
        "platform": 2,
        "accountType": 1,
        "token": "",
        "shopId": shopid,
        "userType": 1,
        "source": 4,
        "content": {
          "openid": openid,
          "username": nickname,
          "shopId": shopid,
          "image": head
        }
      },
      method: "POST",
      header: {
        "Content-Type": "application/JSON"
      },
      success: function (res) {
        var userId = res.data.userId
        wx.setStorageSync('userId', userId)
        // var userIds = wx.getStorageSync('userId')
        // console.log(userIds)
        wx.showTabBar({

        })
        that.setData({
          pop: false
        })
      }
    })
  },
  search: function(e) {
    wx.navigateTo({
      url: 'search/search',
    })
  },
  goto_MeteorFall:function(e){
    wx.navigateTo({
      url: 'MeteorFall/MeteorFall',      
    })
  },
  goto_BeautyKilling:function(e){
    wx.navigateTo({
      url: 'BeautyKilling/BeautyKilling',
    })
  },
  goto_Star_details:function(e){
    var activityid = e.currentTarget.dataset.activityid
    var wareid = e.currentTarget.dataset.wareid
    wx.navigateTo({
      url: 'MeteorFall/Star_details/Star_details?wareId='+wareid+'&activityId='+activityid,
    })
  },
  goto_kill_details:function(e){
    var activityid = e.currentTarget.dataset.activityid
    var wareid = e.currentTarget.dataset.wareid
    wx.navigateTo({
      url: 'BeautyKilling/kill_details/kill_details?wareId='+wareid+'&activityId='+activityid,
    })
  },
  goto_details:function(e){
    var tag = e.currentTarget.dataset.tag;
    var activityid = e.currentTarget.dataset.activityid
    var wareid = e.currentTarget.dataset.wareid
    if(tag==2){
      wx.navigateTo({
        url: 'BeautyKilling/kill_details/kill_details?wareId=' + wareid + '&activityId=' + activityid,
      })
    }else{
      wx.navigateTo({
        url: 'MeteorFall/Star_details/Star_details?wareId=' + wareid + '&activityId=' + activityid,
      })
    }
  },
  // onShow:function(){
  //   var time_ss = getNowFormatDate()
  //   wx.setStorageSync('time_ss', time_ss)
  // },
})
